from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase

from Products.CMFPlone.utils import getToolByName

class OrganizationMembers(ViewletBase):
    
    render = ViewPageTemplateFile("templates/organization_members.pt")
    
    def update(self):
        self.organization_members = self.get_organization_members()
    
    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog', None)
        
    @property 
    def reference_catalog(self):
        return getToolByName(self.context, 'reference_catalog', None)
    
    def get_organization_members(self):
        uid_here = self.context.UID()
        member_brains = []
        if self.reference_catalog is not None:
            referenced_objects = \
                            self.reference_catalog(targetUID = uid_here)
            lookup_uids = [ritem.sourceUID 
                           for ritem in referenced_objects]
            if self.portal_catalog is not None:
                return self.portal_catalog(UID = lookup_uids)
                
        return member_brains
